【スニペット】Obsidian デイリーノートを開く
2024/10/24
今日日付のデイリーノートを開く
code:js
<%*
// 今日の日付をフォーマットして返す関数 (例: "2024-10-24")
function getTodayDate() {
const today = new Date();
const year = today.getFullYear();
const month = String(today.getMonth() + 1).padStart(2, '0');
const day = String(today.getDate()).padStart(2, '0');
return ${year}-${month}-${day};
}
// 今日日付のデイリーノートを開く関数
async function openTodayDailyNote() {
const todayDate = getTodayDate();
const fileName = ${todayDate}.md; // デイリーノートのファイル名を生成
const dailyNotesFolder = 'notes'; // デイリーノートのフォルダパス(必要に応じて変更)
// 現在開いているノートを確認
const currentFile = this.app.workspace.getActiveFile();
if (currentFile) {
const currentFileName = currentFile.basename;
// 現在開いているノートが今日のデイリーノートであれば、何もしない
if (currentFileName === todayDate) {
console.log('今日日付のデイリーノートを既に開いています');
return;
}
}
// 今日日付のデイリーノートを開く
const todayFilePath = ${dailyNotesFolder}/${fileName};
const fileExists = await this.app.vault.adapter.exists(todayFilePath);
if (fileExists) {
// ノートが既に存在する場合、開く
const todayFile = await this.app.vault.getAbstractFileByPath(todayFilePath);
this.app.workspace.activeLeaf.openFile(todayFile);
} else {
// ノートが存在しない場合、作成して開く
const todayFile = await this.app.vault.create(todayFilePath, # ${todayDate}\n);
this.app.workspace.activeLeaf.openFile(todayFile);
}
}
// コードを実行
openTodayDailyNote();
%>